package com.lvbaoins.creditapp.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Arrays;

/**
 * @author : gjb
 * @description:
 * @date : 2020-10-23 11:41
 * @version: : v1.0
 */
@Component
public class MailCompent {


    private static Logger log= LoggerFactory.getLogger(MailCompent.class);

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.send}")
    private boolean sendFlag;

    /**
     * 发送普通文本邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendSimpleMail(String[] to, String subject, String content) {

        boolean flag = true;
        String failReason = "";
        try {
            log.info("发送邮件开始:{},{},{}", to, subject, content);

            if (sendFlag) {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setTo(to);//收信人
                message.setSubject(subject);//主题
                message.setText(content);//内容
                message.setFrom(from);//发信人
                mailSender.send(message);
            } else {
                content += "(不发送真实邮件)";
            }
        } catch (Exception e) {
            log.error("发送邮件错误:", e);
            flag = false;
            failReason = e.getMessage();
        }
        this.insertMail(Arrays.toString(to), subject, content, flag, failReason);
        log.info("发送邮件成功,收件人:{}", Arrays.toString(to));
    }

    /**
     * 发送HTML邮件
     */
    public void sendHtmlMail(String[] to, String subject, String content) {
        log.info("发送邮件开始:{},{},{}", to, subject, content);
        //使用mimeMessage,mime协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            //true代表支持html
            helper.setText(content, true);
            mailSender.send(message);
            log.info("发送邮件成功");
        } catch (Exception e) {
            log.error("发送html邮件失败:", e);
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param to       收件人
     * @param cc       抄送
     * @param subject  主题
     * @param content  内容
     * @param fileName 文件名
     * @param fileInputStream 附件流
     */
    public void sendAttachmentMail(String to,String[] cc, String subject, String content,String fileName, byte[] bytes) {

        log.info("发送带附件邮件开始：{},{},{}", to, subject, content);
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            //true代表支持多组件，如附件，图片等
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            if(cc != null){
                helper.setCc(cc);
            }
            if(bytes != null){
                helper.addAttachment(fileName, new ByteArrayResource(bytes));//添加附件，可多次调用该方法添加多个附件
            }
            mailSender.send(message);
            log.info("发送带附件邮件成功");
        } catch (Exception e) {
            log.error("发送带附件邮件失败", e);
        }
    }

    /**
     * 发送带图片的邮件
     *
     * @param to      收件人
     * @param subject 主题
     * @param content 文本
     * @param rscPath 图片路径
     * @param rscId   图片ID，用于在<img>标签中使用，从而显示图片
     */
    public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {

        log.info("发送带图片邮件开始：{},{},{},{},{}", to, subject, content, rscPath, rscId);
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);//重复使用添加多个图片
            mailSender.send(message);
            log.info("发送带图片邮件成功");
        } catch (Exception e) {
            log.error("发送带图片邮件失败", e);
        }
    }


    /**
     * 保存邮件记录
     *
     * @param to
     * @param subject
     * @param content
     * @param flag
     * @param failReason
     */
    public void insertMail(String to, String subject, String content, boolean flag, String failReason) {
      //不进行保存
    }
}
